package com.jiaoyou.baojian.activity;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiaoyou.baojian.BaojianApplication;
import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.agora.AgoraHelper;
import com.jiaoyou.baojian.entity.InviteExtra;
import com.jiaoyou.baojian.entity.UserInfo;
import com.jiaoyou.baojian.entity.response.RoomItemResponse;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback;
import com.jiaoyou.baojian.otto.Event;
import com.jiaoyou.baojian.utils.Utils;
import com.squareup.otto.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * 呼入
 */
public class CallinActivity extends BaseActivity {

    @BindView(R.id.from_user_header)
    ImageView fromUserHeader;
    @BindView(R.id.from_user_name)
    TextView fromUserName;
    @BindView(R.id.refuse)
    LinearLayout refuse;
    @BindView(R.id.answer)
    LinearLayout answer;
    @BindView(R.id.tip)
    TextView tip;

    private InviteExtra inviteExtra;
    private UserInfo userInfo;
    private String channelId;
    private String account;

    private MediaPlayer mediaPlayer;
    private Vibrator vibrator;

    private RoomItemResponse roomItemResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        Utils.wakeUpAndUnlock(this);

        needOtto();

        isCallBusy = true;

        String extra = getIntent().getStringExtra("extra");
        channelId = getIntent().getStringExtra("channelId");
        account = getIntent().getStringExtra("account");
        inviteExtra = getGson().fromJson(extra, InviteExtra.class);
        userInfo = inviteExtra.userInfo;
        fromUserName.setText(userInfo.nickname);
        tip.setText(userInfo.nickname + "请求通话");
        Utils.showCircleImage(CallinActivity.this, userInfo.avatar, fromUserHeader);

        mediaPlayer = MediaPlayer.create(this, R.raw.music);
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        long [] pattern = {1000, 1000, 1000, 1000};   // 停止 开启 停止 开启
        vibrator.vibrate(pattern, 2);

        BaojianApplication.getInstance().pushService.clearNotifications();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        isCallBusy = false;
        vibrator.cancel();
        if(mediaPlayer != null){
            mediaPlayer.stop();
        }
    }

    @Override
    public int getContentView() {
        return R.layout.activity_callin;
    }

    @OnClick({R.id.refuse, R.id.answer})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.refuse:
                AgoraHelper.getInstance().agoraAPI.channelInviteRefuse(channelId, account, 0, "");
                finish();
                break;
            case R.id.answer:
                //生成语音频道string，发送给对方，对方返回加入成功，则进入通话界面
                toast("接通中，请稍后...");
                answer.setEnabled(false);
                //通知服务器开始点对点聊天，实时开启扣款
                Api.getInstance().pickup(channelId, userInfo.userId,
                        new BaofangCallback<RoomItemResponse>() {
                    @Override
                    public void onFailure(Call call, Throwable t) {
                        toast("创建对话失败");
                        AgoraHelper.getInstance().agoraAPI.channelInviteRefuse(channelId, account, 0, "");
                        finish();
                    }

                    @Override
                    public void onSuccess(RoomItemResponse roomItemResponse) {
                        AgoraHelper.getInstance().agoraAPI.channelInviteAccept(channelId, account, 0);
                        CallinActivity.this.roomItemResponse = roomItemResponse;
                        Intent intent1 = new Intent(CallinActivity.this, TalkingActivity.class);
                        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent1.putExtra("actress", true);
                        intent1.putExtra("channel_name", channelId);
                        intent1.putExtra("channel_key", roomItemResponse.agoraChannelKey);
                        intent1.putExtra("room", roomItemResponse);
                        intent1.putExtra("userInfo", userInfo);
                        startActivity(intent1);
                        finish();
                    }
                });
                break;
        }
    }

    @Subscribe
    public void onIMOrder(String event){
        if(event.equals(Event.AGORA_INVITE_END_BY_PEER)) {
            toast("对方取消");
            finish();
        }
    }
}
